﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogLib;
using System.Threading;
using Newtonsoft.Json.Linq;
using DeviceCommon;
using System.IO;

namespace QrCode
{
    public class QrCode : DeviceBase
    {
        private Polling _poll = null;

        private QrCodeOption _option = new QrCodeOption();
        public QrCode()
        {
            InitOption();
        }

        private void InitOption()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }

        public override void Init(string param, out string outParam)
        {
            outParam = "";         
            Log.Write("Start Init");          
            string PortName = _option.PortName;
            int BaudRate = _option.BaudRate;       
            Log.Write(string.Format("配置的二维码扫描器摄像头的COM口[{0}]波特率[{1}]", PortName, BaudRate));
            if (ScanCode.InitCom(PortName, BaudRate))
            {
                Log.Write("二维码扫描器初始化成功");
                _bInited = true;               
            }
            else
            {
                _bInited = false;
                Log.Write("二维码扫描器初始化失败");
            }         
            Log.Write("End Init");
        }

        public override bool DoAction(string param, out string outParam)
        {
            outParam = "";
            _error = "";
            ScanCode.barcodeMsg = "";
            if (_bInited)
            {
                _poll = new Polling(DetectQrCode, param);
                _poll.TimeoutMilliSecond = TimeoutMillisecond;
                _poll.ScanInterval = _option.ScanInterval;
                _poll.Start(OnWorkingCompleted);
                return true;
            }
            else
            {
                _error = "读写器未初始化";
                Log.Write("读写器未初始化");
                OnWorkingFailed("读写器未初始化");
                return false;
            }
        }

        public override void Start(string param, out string outParam)
        {
            outParam = "";
            ScanCode.barcodeMsg = "";
            if (_bInited)
            {
                _poll = new Polling(DetectQrCode, param);
                _poll.TimeoutMilliSecond = TimeoutMillisecond;
                _poll.ScanInterval = _option.ScanInterval;
                _poll.Start(OnWorkingCompleted);
            }
            else
            {
                Log.Write("读写器未初始化");
                OnWorkingFailed("读写器未初始化");
            }
        }

        public override void Stop(string param, out string outParam)
        {
            outParam = "";
            if (_poll != null)
            {
                _poll.Stop();
                _poll.Dispose();
                _poll = null;
            }
        }

        public override void Exit(string inParams, out string outParam)
        {
            outParam = "";
            try
            {
                Log.Write("Start Exit");
                outParam = "";
                if (_bInited)
                {
                    ScanCode.CloseCom();
                }
                Log.Write("End Exit");
                _state = DeviceState.NotInitialized;
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
            }
            finally
            {
                _bInited = false;
                _bExited = true;
            }
        }

        public override void ShowConfigDialog(string inParam)
        {
        }
   
        private bool DetectQrCode(string inParams, out string outParam)
        {
            outParam = "";
            if (!string.IsNullOrEmpty(ScanCode.barcodeMsg))
            {
                string resultStr = ScanCode.barcodeMsg;
                JObject param = new JObject();
                param.Add("QrCode", resultStr);
                outParam = param.ToString();
                Log.Write("识别成功:" + resultStr);
                return true;
            }
            Thread.Sleep(_option.ScanInterval);
            return false;
        }
    }
}
